package SwingView.Lecture;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;

import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsLecture;
import SwingView.CmsContentPanelModel;

public class CmsCPInstructorLecture extends CmsContentPanelModel{

	private JLabel jlTitle;
	private JPanel jpContent;
	private LectureUploadPanel jpUpload;
	
	
	public CmsCPInstructorLecture(){
		
		//instantiate People title
		jlTitle = new JLabel("Lecture Materials");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setAlignmentY(CENTER_ALIGNMENT);
		jlTitle.setAlignmentY(CENTER_ALIGNMENT);
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		
		//instantiate upload panel:
		jpUpload = new LectureUploadPanel();
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(600,250));
		jp.setOpaque(false);
		jp.setLayout( new FlowLayout(FlowLayout.CENTER));
		jp.setBorder(BorderFactory.createEmptyBorder());
		jp.add(jlTitle);
		jp.add(jpUpload); 
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);
		
		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);
		
		//set up student lecture panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		add(scroll,BorderLayout.WEST);//Add People List
	}
	
	
	public LectureUploadPanel getLectureUploadPanel(){
		return jpUpload;
	}
	
	//---------------------------------------------------------
	//METHOD: setLectureData
	//---------------------------------------------------------
	/**
	 * Set the displaying list of lectures
	 * Make sure you update the list from the DB server before 
	 * feeding the list to this method
	 * @param listOfLecture
	 */
	public void displayListOfLecture(List<CmsLecture> listOfLecture){
		jpContent.removeAll();
		jpContent.revalidate();
		jpContent.repaint();
		int i = 0;
		for(CmsLecture lecture : listOfLecture){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0, 0, 8, 0);
			
			jpContent.add(new LectureEntryRow(lecture),c);
			
			i++;
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = i;
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
		jpContent.revalidate();
	}
	
	public void addALLecturePost(ALLecturePost alLecturePost){
		jpUpload.addALLecturePost(alLecturePost);
	}
	
	public File getSelectedFile(){
		return jpUpload.getSelectedFile();
	}
	
	public Date getSelectedAvaiDate(){
		return jpUpload.getSelectedAvaiDate();
	}
	
	public Date getSelectedExpDate(){
		return jpUpload.getSelectedExpDate();
	}
	
	//---------------------------------------------------------
	//CLASS: Lecture Row Entry
	//---------------------------------------------------------
	private class LectureEntryRow extends JPanel{
		final static private int ROW_WIDTH = 550;
		final static private int ROW_HEIGHT = 70;
		
		final static private int DATE_NAME_COLUMN_SIZE = 100;
		
		
		public LectureEntryRow(CmsLecture lecture){
			JLabel fname;
			JLabel avaiDate; //avai date
			JLabel adTitle;
			JLabel expDate; //exp date
			JLabel edTitle;
			JButton dlButton;
			JButton deleteButton;
			SimpleDateFormat sdf;
			
			//Set up file name label
			String filename = lecture.getLectureFile().getName();
			fname = new JLabel(filename);
			fname.setFont(fname.getFont().deriveFont(15.0f).deriveFont(Font.BOLD));
			fname.setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 5));
			
			//Set up the "Available date" title label
			adTitle = new JLabel("Available Date: ");
			adTitle.setFont(adTitle.getFont().deriveFont(10.0f).deriveFont(Font.BOLD));
			adTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 0, 5));
			
			//set up avai date Label
			sdf = new SimpleDateFormat("MMM dd YYYY,  HH:mm");
			System.out.println("AvaiDate: " + new Date(lecture.getAvaiDate().getTime()).toString());
			avaiDate = new JLabel(sdf.format(new Date(lecture.getAvaiDate().getTime())));
			avaiDate.setFont(fname.getFont().deriveFont(10.0f));
			avaiDate.setForeground(new Color(40,40,40));
			avaiDate.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
			
			//Set up the "Expiration Date" title label:
			edTitle = new JLabel("Expiration Date: ");
			edTitle.setFont(edTitle.getFont().deriveFont(10.0f).deriveFont(Font.BOLD));
			edTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 8, 5));
			
			//set up the exp date Label
			Date tempExpDate = new Date(lecture.getExpDate().getTime());
			if(tempExpDate.compareTo(new Date(CmsPresenter.DEFAULT_EXP_DATE.getTime())) == 0){
				expDate = new JLabel("No Expiration Date");
			}
			else expDate = new JLabel(sdf.format(tempExpDate));
			expDate.setFont(fname.getFont().deriveFont(10.0f));
			expDate.setForeground(new Color(40,40,40));
			expDate.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 5));
			
			//set up download button
			dlButton = new JButton ("Download");
			dlButton.setFont(dlButton.getFont().deriveFont(12.0f));
			dlButton.setForeground(Color.BLUE);
			dlButton.setOpaque(false);
			dlButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			dlButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			dlButton.addActionListener(new ALDownloadFile(lecture.getLectureFile()));
			
			//set up delete button
			deleteButton = new JButton ("Delete");
			deleteButton.setFont(dlButton.getFont().deriveFont(12.0f));
			deleteButton.setForeground(Color.BLUE);
			deleteButton.setOpaque(false);
			deleteButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			deleteButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			deleteButton.addActionListener(new ALLectureDelete(lecture));
			
			//Set up the panel
			setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setBackground(Color.WHITE);
			setBorder(BorderFactory.createLineBorder(new Color(170,170,170), 1));
			setOpaque(true);
			setLayout(new GridBagLayout());
			
			//add the File name
			GridBagConstraints cf = new GridBagConstraints();
			cf.fill = GridBagConstraints.HORIZONTAL;
			cf.anchor = GridBagConstraints.FIRST_LINE_START;
			cf.weightx = 1.0;
			cf.gridwidth = 4;
			add(fname,cf);
			
			//add "Available date" title:
			GridBagConstraints cp = new GridBagConstraints();
			cp.gridx = 0;
			cp.gridy = 1;
			cp.anchor = GridBagConstraints.LAST_LINE_START;
			add(adTitle,cp);
			
			//add the avai date
			GridBagConstraints cd = new GridBagConstraints();
			cd.gridx = 1;
			cd.gridy = 1;
			cd.anchor = GridBagConstraints.LAST_LINE_START;
			cd.weighty = 1.0;
			add(avaiDate,cd);
			
			//add "Expiration date" title:
			GridBagConstraints ce = new GridBagConstraints();
			ce.gridx = 0;
			ce.gridy = 2;
			ce.anchor = GridBagConstraints.LAST_LINE_START;
			add(edTitle,ce);
			
			//add the exp date
			GridBagConstraints cx = new GridBagConstraints();
			cx.gridx = 1;
			cx.gridy = 2;
			cx.anchor = GridBagConstraints.LAST_LINE_START;
			add(expDate,cx);
			
			//add the download button
			GridBagConstraints cb = new GridBagConstraints();
			cb.gridx = 3;
			cb.gridy = 1;
			cb.gridheight = 2;
			cb.anchor = GridBagConstraints.LAST_LINE_END;
			add(dlButton,cb);
			
			//add the delete button
			GridBagConstraints cdel = new GridBagConstraints();
			cdel.gridx = 2;
			cdel.gridy = 1;
			cdel.gridheight = 2;
			cdel.weightx = 1.0;
			cdel.anchor = GridBagConstraints.LAST_LINE_END;
			add(deleteButton,cdel);
		}
	}

}
